package com.gmscosmo.janus.lock;


import com.gmscosmo.janus.util.JanusLogUtil;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * ClassName: RedissonConfig
 * Description:
 * Date: 2020/11/9 10:38
 *
 * @author stephen.qiu
 */
@Configuration
@ConditionalOnClass(Config.class)
public class RedissonConfig {
    @Autowired
    JanusLogUtil janusLogUtil;
    @Autowired
    RedissonProperties redissonProperties;

    /**
     * 哨兵模式自动装配
     * @return
     */
//    @Bean
//    @ConditionalOnProperty(name="redisson.master-name")
//    RedissonClient redissonSentinel() {
//        Config config = new Config();
//        SentinelServersConfig serverConfig = config.useSentinelServers().addSentinelAddress(redssionProperties.getSentinelAddresses())
//                .setMasterName(redssionProperties.getMasterName())
//                .setTimeout(redssionProperties.getTimeout())
//                .setMasterConnectionPoolSize(redssionProperties.getMasterConnectionPoolSize())
//                .setSlaveConnectionPoolSize(redssionProperties.getSlaveConnectionPoolSize());
//
//        if(StringUtils.isNotBlank(redssionProperties.getPassword())) {
//            serverConfig.setPassword(redssionProperties.getPassword());
//        }
//        return Redisson.create(config);
//    }

//    /**
//     * 单机模式自动装配
//     * @return
//     */
//    @Bean
//    RedissonClient redissonSingle() {
//        Config config = new Config();
//        SingleServerConfig serverConfig = config.useSingleServer()
//                .setAddress("redis://"+redissonProperties.getHost()+":"+redissonProperties.getPort())
//                .setTimeout(redissonProperties.getTimeout())
//                .setConnectionPoolSize(redissonProperties.getConnectionPoolSize())
//                .setConnectionMinimumIdleSize(redissonProperties.getConnectionMinimumIdleSize());
//
//        if(redissonProperties.getPassword() != null) {
//            serverConfig.setPassword(redissonProperties.getPassword());
//        }
//        janusLogUtil.log("单机RedissonClient装配成功",this.getClass(),JanusLogUtil.INFO);
//        return Redisson.create(config);
//    }

    /**
     * 集群模式自动装配
     *
     * @return
     */
    @Bean
    RedissonClient redissonCluster() {
        Config config = new Config();
        ClusterServersConfig serverConfig = config.useClusterServers()
                .setTimeout(redissonProperties.getTimeout())
                .setMasterConnectionPoolSize(redissonProperties.getConnectionPoolSize())
                .setMasterConnectionMinimumIdleSize(redissonProperties.getConnectionMinimumIdleSize());
        for (String node : redissonProperties.getClusterNodes().split(",")) {
            serverConfig.addNodeAddress("redis://"+node);
        }
        if (redissonProperties.getPassword() != null) {
            serverConfig.setPassword(redissonProperties.getPassword());
        }
        janusLogUtil.log("集群RedissonClient装配成功", this.getClass(), JanusLogUtil.INFO);
        return Redisson.create(config);
    }

    /**
     * 装配locker类，并将实例注入到RedissLockUtil中
     *
     * @return
     */
    @Bean
    RedisLockerService setRedisLockerService(RedissonClient redissonClient) {
        RedisLockerService redisLockerService = new RedisLockerServiceImpl();
        redisLockerService.setRedissonClient(redissonClient);
        RedissLockUtil.setLocker(redisLockerService);
        janusLogUtil.log("RedisLockerService装配成功", this.getClass(), JanusLogUtil.INFO);
        return redisLockerService;
    }
}
